package com.app.view.component.dialogs 
{
	import flash.display.SimpleButton;
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.utils.ByteArray;
	
	import flash.geom.Rectangle;
	
	import com.adobe.images.JPGEncoder;
	import com.peachcastle.tools.Base64;
	
	import org.component.Button;
	
	import com.peachcastle.tools.GameCamera;
	import com.peachcastle.core.Dialog;
	import com.app.view.manager.DialogManagerMediator;
	import com.app.view.AppMediator;
	
	import com.app.view.component.dialogs.feedBackView.SendBug;
	
	import com.peachcastle.debug.Log;
	
	/**
	 * ...
	 * @author kevin
	 */
	public class FeedBackView extends Dialog
	{
		private var _dialogManagerMediator:DialogManagerMediator = null;
		
		private var _btn_close:Button = null;
		private var _btn_cancel:Button = null;
		private var _btn_ok:Button = null;
		private var _gameCamera:GameCamera = null;					//照相机	
		private var _layer:DisplayObject = null;					//拍照层	
		private var _sendbug:SendBug = null;
		
		private var _tempbmp:Bitmap = null;
		
		public function FeedBackView(dialogUrlFunc:Function, dialogManagerMediator:DialogManagerMediator) 
		{
			_dialogManagerMediator = dialogManagerMediator;
			super("FeedBackView", true, dialogUrlFunc,
					_dialogManagerMediator.stageSize["width"], _dialogManagerMediator.stageSize["height"]);
		}
		
		override protected function initialization():void 
		{
			super.initialization();
			super.move(centerX, 100);
			
			_btn_close = new Button(assest["btn_close"]);
			_btn_cancel = new Button(assest["btn_cancel"]);
			_btn_cancel.setLabel("關閉")
			_btn_ok = new Button(assest["btn_ok"]);
			_btn_ok.setLabel("提交");
			_sendbug = new SendBug(assest["mc_sendbug"]);
			_sendbug.setSubject(this);
			_gameCamera = new GameCamera();
		}
		
		override protected function configureEventListeners():void 
		{
			super.configureEventListeners();
			
			_btn_close.addEventListener(MouseEvent.CLICK, onClose);
			_btn_cancel.addEventListener(MouseEvent.CLICK, onClose);
			_btn_ok.addEventListener(MouseEvent.CLICK,onOk);
		}
		
		private function onClose(e:MouseEvent):void 
		{
			super.hide();
		}
		
		private function onOk(e:MouseEvent):void 
		{
			var type:int = _sendbug.tabSelectIndex + 1;
			_dialogManagerMediator.sendNotification(AppMediator.FEEDBACK_SEND_INFO_REQUEST, [{'type': type, 'title': _sendbug.titleTips, 'input': _sendbug.inputText, 'callback': sendFeedBackAffix}]);

			super.hide();
		}
		
		private function sendFeedBackAffix(obj:Object):void
		{
			if (obj && obj["flag"] == 1)
			{
				var _files:String = '';
				if (_sendbug.upLoadSelect == true)
				{
					var jpgEncoder:JPGEncoder = new JPGEncoder(15);
					var _bytes:ByteArray = jpgEncoder.encode(_tempbmp.bitmapData);
					_files = Base64.encodeByteArray(_bytes);
				}
				
				_dialogManagerMediator.sendNotification(AppMediator.FEEDBACK_SEND_AFFIX_REQUEST, 
																[{'fid': obj["fid"], 'image': _files}]);
			}
		}
		
		override protected function showAction():void 
		{
			super.showAction();	
			
			_sendbug.initText();
			_btn_ok.setDisable(true);
			
			_tempbmp = _gameCamera.takePhoto(data['layer']);
			_sendbug.setScreenShot(_tempbmp);
		}
		
		public function get btn_ok():Button { return _btn_ok; }
	}

}